import {
  Component,
  EventEmitter,
  Injector,
  Input,
  OnInit,
  Output,
} from '@angular/core';
import {
  GetEnumValuesListDto,
  CommonServiceProxy,
} from '@abp/service-proxies/service-proxies';
import { AppComponentBase } from '@abp/components';

@Component({
  selector: 'enum-combo',
  templateUrl: './enum-combo.component.html',
  styles: [],
})
export class EnumComboComponent extends AppComponentBase implements OnInit {
  @Output() selectedValueChange: EventEmitter<
    GetEnumValuesListDto
    > = new EventEmitter<GetEnumValuesListDto>();
  //枚举类型全名
  @Input() fullName = '';
  values: GetEnumValuesListDto[] = [];
  @Input() placeholder: string;
  @Input() selectedEnum: GetEnumValuesListDto = undefined;

  private _enumId: number;
  @Input() enumId: number;

  constructor(private _commonService: CommonServiceProxy, injector: Injector) {
    super(injector);
  }

  ngOnChanges(): void {
    this._commonService.getEnumValuesList(this.fullName).subscribe(result => {
      this.values = result;
      this._enumId = this.enumId;
      this.autoSelect();
    });
  }

  ngOnInit(): void { }

  autoSelect() {
    if (!this.values && !this._enumId) {
      return;
    }
    this.selectedEnum = this.values.find(item => item.value == this._enumId);
    if (this.selectedValueChange) {
      this.selectedValueChange.emit(this.selectedEnum);
    }
  }
}
